package cn.itcast.service.impl;

import cn.itcast.dao.OrderSettingDao;
import cn.itcast.entity.OrderSettingVo;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.pojo.Params;
import cn.itcast.service.OrderSettingService;
import com.alibaba.dubbo.config.annotation.Service;
import io.netty.util.internal.ObjectUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;


    @Override
    public List<OrderSettingVo> findOrderByMonth(String  date) {

        try {
            //设置时间
            DateFormat fmt =new SimpleDateFormat("yyyy-MM");
            Date date2 = fmt.parse(date);
            String time = fmt.format(date2);

            //查询预约设置
            List<OrderSetting> orderSettingList = orderSettingDao.findOrderByMonth(time);



            List<OrderSettingVo> orderVoList = new ArrayList<>();

            DateFormat fmt2 =new SimpleDateFormat("yyyy-MM-dd");

            for (OrderSetting orderSetting : orderSettingList) {
                //创建视图对象
                OrderSettingVo orderSettingVo = new OrderSettingVo();

                Date orderDate = orderSetting.getOrderDate();
                String str = fmt2.format(orderDate);

                String[] split = str.split("-");

                //填充
                orderSettingVo.setDate(split[2]);
                orderSettingVo.setNumber(orderSetting.getNumber());
                orderSettingVo.setReservations(orderSetting.getReservations());

                orderVoList.add(orderSettingVo);
            }

            //System.out.println(orderVoList);

            return orderVoList;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 添加预约
     * @param params
     * @return
     */
    @Override
    public Integer OrderSettingNumber(Params params) {
        //获取添加的时间
        String orderDate = params.getOrderDate();
        OrderSetting result = orderSettingDao.findByDate(orderDate);

        //查看预约人数
        int count = this.findOrder(orderDate);


        //如果预约数不为0
         params.setReservations(count);

        //判断预约设置是否存在
            if (result==null){

                return orderSettingDao.saveNumber(params);
            }

                return orderSettingDao.updateNumber(params);


    }


    /**
     * 批量添加
     * @return
     */
    @Override
    public void save(List<Params> ParamsList) {

            for (Params param : ParamsList) {
                //查看是否有重复数据
                OrderSetting result = orderSettingDao.findByDate(param.getOrderDate());

                //查看预约人数
                int count = this.findOrder(param.getOrderDate());

                //如果预约数不为0
                    param.setReservations(count);


                //如果不重复
                if (result==null){
                    orderSettingDao.save(param);
                }
               orderSettingDao.updateNumber(param);
            }

    }

    /**
     * 查询预约人数
     */
    public int findOrder(String date){
        return orderSettingDao.findOrder(date);
    }



}
